﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using FinMkts.CFD.Data;

namespace FinMkts.Price.Simulator
{
    public static class Program
    {
        private const string ProviderCode = "INT";

        public static void Main(string[] args)
        {
            var catalog = new DirectoryCatalog(".");
            var container = new CompositionContainer(catalog);
            var simulator = new MarketDataSimulator();
            container.ComposeParts(simulator);

            Console.WriteLine("Simulate (R)andom markets, or any other key for markets defined the database");
            var key = Console.ReadKey();

            IEnumerable<string> listings = key.Key == ConsoleKey.R ? GetRandomListings(50 * 1000) : GetListingsFromDB();

            simulator.ProviderCode = ProviderCode;
            simulator.DataCodes = listings;
            simulator.Start();

            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }

        private static IEnumerable<string> GetRandomListings(int count)
        {
            for (int i = 1; i <= count; i++)
            {
                yield return i.ToString("X");
            }
        }

        private static IEnumerable<string> GetListingsFromDB()
        {
            Console.WriteLine("reading db...");
            List<string> listings;
            using (var context = new CfdModelContainer())
            {
                listings = (from mdl in context.MarketDataListings
                            where mdl.MarketDataProvider.Code == ProviderCode
                            select mdl.DataCode).ToList();
            }
            return listings;
        }
    }
}